package com.acce.gxsc.page.work;

import com.acce.gxsc.model.WorkModel;
import com.acce.gxsc.model.loaclBean.DutyLogBean;
import com.acce.gxsc.model.loaclBean.ResultBean;
import com.acce.gxsc.model.netBean.DutyBean;
import com.acce.gxsc.model.netBean.NetWorkAreaListBean;
import com.acce.sdk.base.mvp.BasePresenter;
import com.acce.sdk.utils.ToastUtils;

import rx.Subscriber;

/**
 * Created by huxley on 2017/5/26.
 */

public class WorkPresenter  extends BasePresenter<WorkView> {

    public void loadWork() {
        if (mViewRef != null) {
            WorkModel.getInstance().loadWork().subscribe(new Subscriber<ResultBean<DutyLogBean>>() {
                @Override
                public void onStart() {
                    if (mViewRef != null) {
                        getView().showProgressDialog("加载中...", false);
                    }
                }
                @Override
                public void onNext(ResultBean<DutyLogBean> datas) {
                    if (mViewRef != null) {
                        if (datas.result == null || datas.result.DutyLog == null || datas.result.DutyLog.dutyId == null) {
                            getView().setFirstWork(datas.returnMsg);
                        } else {
                            getView().loadWorkSuccess(datas.result.DutyLog);
                        }
                    }
                }
                @Override
                public void onCompleted() {
                    if (mViewRef != null) {
                        getView().hideProgressDialog();
                    }
                }
                @Override
                public void onError(Throwable e) {
                    if (mViewRef != null) {
                        e.printStackTrace();
                        getView().hideProgressDialog();
                    }
                }
            });
        }
    }

    public void startWork(String gateNo, String workNo) {
        WorkModel.getInstance().startWork("1", gateNo, workNo).subscribe(new Subscriber<ResultBean<DutyBean>>() {
            @Override
            public void onStart() {
                if (mViewRef != null) {
                    getView().showProgressDialog("加载中...", false);
                }
            }
            @Override
            public void onNext(ResultBean<DutyBean> datas) {
                ToastUtils.show(datas.returnMsg);
                getView().startWorkSuccess(datas.result);
            }
            @Override
            public void onCompleted() {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                }
            }
            @Override
            public void onError(Throwable e) {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                    ToastUtils.show("加载失败！");
                }
            }
        });
    }

    public void workAgain(String dutyId) {
        WorkModel.getInstance().workAgain(dutyId).subscribe(new Subscriber<ResultBean<DutyBean>>() {
            @Override
            public void onStart() {
                if (mViewRef != null) {
                    getView().showProgressDialog("加载中...", false);
                }
            }
            @Override
            public void onNext(ResultBean<DutyBean> datas) {
                ToastUtils.show(datas.returnMsg);
                getView().workAgainSuccess(datas.result);
            }
            @Override
            public void onCompleted() {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                }
            }
            @Override
            public void onError(Throwable e) {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                    ToastUtils.show("加载失败！");
                }
            }
        });
    }

    public void loadPositionList(final int i) {
        WorkModel.getInstance().loadWorkAreaList().subscribe(new Subscriber<ResultBean<NetWorkAreaListBean>>() {
            @Override
            public void onStart() {
                if (mViewRef != null) {
                    getView().showProgressDialog("加载中...", false);
                }
            }
            @Override
            public void onNext(ResultBean<NetWorkAreaListBean> datas) {
                if (!"200".equals(datas.returnCode)) {
                    ToastUtils.show(datas.returnMsg);
                    return;
                }
                getView().loadPositionListSuccess(datas.result.workAreaList,i);
            }
            @Override
            public void onCompleted() {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                }
            }
            @Override
            public void onError(Throwable e) {
                if (mViewRef != null) {
                    getView().hideProgressDialog();
                    ToastUtils.show("加载失败！");
                }
            }
        });
    }
}
